<template>
  <div class="box-list-container">
    <div class="box-query-form">
      <el-form :model="queryParam" label-width="auto" label-position="right" :gutter="24">
        <el-row>
          <el-col :xl="6" :lg="6" :md="12" :sm="24">
            <el-form-item label="店铺名称:">
              <el-input v-model="queryParam.sName" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :xl="6" :lg="6" :md="12" :sm="24">
            <el-form-item label="店铺旧名称:">
              <el-input v-model="queryParam.sUsedName" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :xl="6" :lg="6" :md="12" :sm="24">
            <el-form-item label="店铺旺旺:">
              <el-input v-model="queryParam.sWantWant" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <template v-if="toggleSearchStatus && userInfo.userType === '666'">
            <el-col v-if="userInfo.userType === '666'" :xl="6" :lg="6" :md="12" :sm="24">
              <el-form-item label="所属用户:">
                <selectUser v-model.trim="queryParam.UserNum" />
              </el-form-item>
            </el-col>
          </template>
          <el-col :xl="6" :lg="6" :md="12" :sm="24" class="table-search-btn">
            <el-button icon="el-icon-refresh" @click="searchReset">重置</el-button>
            <el-button type="primary" icon="el-icon-search" @click="searchQuery">查询</el-button>
            <el-link v-if="userInfo.userType === '666'" style="margin-left: 10px;" type="primary" :underline="false" @click="toggleSearchStatus = !toggleSearchStatus">
              {{ toggleSearchStatus ? '收起' : '展开' }}
              <i :class="toggleSearchStatus ? 'el-icon-arrow-up' : 'el-icon-arrow-down'" />
            </el-link>
          </el-col>
        </el-row>
      </el-form>
    </div>
    <div class="box-table">
      <div class="table-title">
        <div class="table-title-left">
          店铺管理列表
        </div>
        <div>
          <el-button v-if="userInfo.userType !== '666'" plain class="btnAdd" icon="el-icon-plus" @click="bfADD">新增</el-button>
        </div>
      </div>
      <!-- 批量操作 -->
      <div v-if="selectedRowList.length" class="batchAction">
        <div class="batchAction-left">
          <el-dropdown>
            <el-button>
              批量操作 <i class="el-icon-arrow-down el-icon--right" />
            </el-button>
            <el-dropdown-menu slot="dropdown">
              <el-dropdown-item>
                <div @click="batchDelete"><i class="el-icon-delete" />删除</div>
              </el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
        </div>
        <div class="batchAction-right">
          已选择
          <span style="font-weight: 700">
            <el-link type="primary" :underline="false">{{ selectedRowList.length }}</el-link>
          </span>项
          <el-link type="primary" :underline="false" style="margin-left: 24px;" @click="onClearSelected">清空</el-link>
        </div>
      </div>
      <el-table ref="table" :data="dataSource" @selection-change="onSelectChange">
        <el-table-column type="selection" width="55" />
        <el-table-column v-if="userInfo.userType === '666'" label="所属用户" prop="UserNum" min-width="120" />
        <el-table-column fixed="left" prop="id" label="ID" width="70" />
        <el-table-column prop="sName" label="店铺名称" min-width="200" />
        <el-table-column prop="sUsedName" label="店铺旧名称" min-width="200" />
        <el-table-column prop="sWantWant" label="店铺旺旺" min-width="200" />
        <el-table-column prop="sUrl" label="店铺网址" min-width="300" />
        <el-table-column prop="createTime" label="创建时间" width="170" />
        <el-table-column fixed="right" label="操作" width="150">
          <template slot-scope="scope">
            <el-button type="text" @click="handleEdit('查看', scope.row, true)">查看</el-button>
            <el-button type="text" @click="editData('编辑', scope.row)">编辑</el-button>
            <el-popconfirm title="确定删除吗？" style="margin-left: 10px" @confirm="delData(scope.row[guidFieldName])">
              <el-button slot="reference" type="text" style="color: red;">删除</el-button>
            </el-popconfirm>
          </template>
        </el-table-column>
      </el-table>
      <div class="box-paging">
        <Pagination
          v-show="ipagination.total > 0"
          layout="total, sizes, prev, pager, next"
          :total="ipagination.total"
          :page.sync="ipagination.pageNum"
          :limit.sync="ipagination.pageSize"
          @pagination="loadData"
        />
      </div>
    </div>
    <handleDrawer ref="modalForm" @ok="modalFormOk" />
  </div>
</template>

<script>
import { listMixin } from '@/mixins/listMixin'
import Pagination from '@/components/Pagination'
import handleDrawer from './components/handleDrawer'
import selectUser from '@/components/xm/selectUser/selectUser'
import { postAction } from '@/api/manage'

export default {
  components: {
    Pagination,
    handleDrawer,
    selectUser
  },
  mixins: [listMixin],
  data() {
    return {
      guidFieldName: 'id', // 唯一标识字段
      ipagination: { // 分页参数
        pageNum: 1,
        pageSize: 100,
        total: 0
      },
      toggleSearchStatus: false,
      userInfo: this.$ls.get('USER_INFO') || {},
      url: {
        list: 'storeList.aspx?action=GetList',
        delete: 'storeList.aspx?action=Delete',
        deleteBatch: 'storeList.aspx?action=DeleteBatch'
      }
    }
  },
  created() {},
  mounted() {},
  methods: {
    delData(id) {
      if (this.userInfo.userType === '666' || this.userInfo.isOpenPwdLevel2 !== '1') {
        this.handleDelete(id)
      } else {
        this.$prompt('请输入二级密码', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(({ value }) => {
          this.$store.dispatch('app/setLoading', true)
          postAction('MemberUser.aspx?action=checkPwdLevel2', { pwdLevel2: value }).then((res) => {
            if (res.code === 1) {
              this.handleDelete(id)
              this.$message.success(res.message || '操作成功！')
            } else {
              this.$message.warning(res.message || '操作失败！')
            }
          }).finally(() => {
            this.$store.dispatch('app/setLoading', false)
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          })
        })
      }
    },
    bfADD() {
      if (this.userInfo.userType === '666' || this.userInfo.isOpenPwdLevel2 !== '1') {
        this.handleAdd('新增')
      } else {
        this.$prompt('请输入二级密码', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(({ value }) => {
          this.$store.dispatch('app/setLoading', true)
          postAction('MemberUser.aspx?action=checkPwdLevel2', { pwdLevel2: value }).then((res) => {
            if (res.code === 1) {
              this.handleAdd('新增')
              this.$message.success(res.message || '操作成功！')
            } else {
              this.$message.warning(res.message || '操作失败！')
            }
          }).finally(() => {
            this.$store.dispatch('app/setLoading', false)
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          })
        })
      }
    },
    editData(title, rowData, disableSubmit) {
      if (this.userInfo.userType === '666' || this.userInfo.isOpenPwdLevel2 !== '1') {
        this.handleEdit(title, rowData, disableSubmit)
      } else {
        this.$prompt('请输入二级密码', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消'
        }).then(({ value }) => {
          this.$store.dispatch('app/setLoading', true)
          postAction('MemberUser.aspx?action=checkPwdLevel2', { pwdLevel2: value }).then((res) => {
            if (res.code === 1) {
              this.handleEdit(title, rowData, disableSubmit)
              this.$message.success(res.message || '操作成功！')
            } else {
              this.$message.warning(res.message || '操作失败！')
            }
          }).finally(() => {
            this.$store.dispatch('app/setLoading', false)
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '取消输入'
          })
        })
      }
    },
    handleEdit(title, rowData, disableSubmit) {
      this.$refs.modalForm.edit(rowData)
      this.$refs.modalForm.title = title
      this.$refs.modalForm.disableSubmit = disableSubmit
    }
  }
}
</script>

<style lang="scss" scoped>
</style>

